local skel = fk.CreateSkill {
  name = "lb_zero__jingtuan",
  max_branches_use_time = {
    ["drawcard"] = {
      [Player.HistoryRound] = 1
    },
    ["discard"] = {
      [Player.HistoryRound] = 1
    },
  },
}

Fk:loadTranslationTable {
  ["lb_zero__jingtuan"] = "惊湍",
  [":lb_zero__jingtuan"] = "每轮各限一次，有角色摸牌或弃牌时，你可翻倍之，然后你于本回合结束时执行对应阶段（不能选择与上次相同的选项）。",

  ["#lb_zero__jingtuan_draw"] = "惊湍：%src即将摸%arg张牌，你可以令其多摸%arg张牌，你于本回合结束时执行一个额外的摸牌阶段",
  ["#lb_zero__jingtuan_dis"] = "惊湍：%src即将弃置%arg张牌，你可以令其多弃置%arg张牌，你于本回合结束时执行一个额外的弃牌阶段",
  ["#lb_zero__jingtuan_discard"] = "惊湍：你须多弃置%arg张牌",

  ["@lb_zero__jingtuan"] = "惊湍",

  ["$lb_zero__jingtuan1"] = "别做无谓的抵抗。",
  ["$lb_zero__jingtuan2"] = "你已经惹我生气了。",
  ["$lb_zero__jingtuan3"] = "作为六课的敌人，是要付出代价的。",
}
--- 在当前回合结束后执行额外阶段
---@param player ServerPlayer
local gainAnExtraTurn_end = function(player, extra_phase, name)
  local room = player.room
  local logic = room.logic
  local turn = logic:getCurrentEvent():findParent(GameEvent.Turn, true)
  if turn then
    turn:addExitFunc(function()
      if player:hasSkill(name) then
        local current = player.phase
        player.phase = Player.PhaseNone
        room:broadcastProperty(player, "phase")
        local phase = { ---@type PhaseDataSpec
          who = player,
          reason = name,
          phase = extra_phase,
        }
        GameEvent.Phase:create(PhaseData:new(phase)):exec()
        player.phase = current
        room:broadcastProperty(player, "phase")
      end
    end)
    return
  end
end

skel:addEffect(fk.BeforeDrawCard, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target:isAlive() and data.num > 0
        and skel:withinBranchTimesLimit(player, "drawcard", Player.HistoryRound)
        and player:getMark("@lb_zero__jingtuan") ~= "draw_card"
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player,
      { skill_name = skel.name, prompt = "#lb_zero__jingtuan_draw:" .. target.id .. "::" .. data.num })
  end,
  on_use = function(self, event, target, player, data)
    player:addSkillBranchUseHistory(skel.name, "drawcard")
    player.room:setPlayerMark(player, "@lb_zero__jingtuan", "draw_card")
    data.num = data.num * 2
    gainAnExtraTurn_end(player, Player.Draw, skel.name)
  end,
}, { check_skill_limit = true })

skel:addEffect(fk.BeforeCardsMove, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) 
    or not skel:withinBranchTimesLimit(player, "discard", Player.HistoryRound)
    or player:getMark("@lb_zero__jingtuan") == "discard" then
      return
    end
    for i, move in ipairs(data) do
      local cards = {}
      if move.moveReason == fk.ReasonDiscard and move.from and move.proposer == move.from and move.from:isAlive() then --自己弃自己的牌
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Player.Hand or info.fromArea == Player.Equip then
            table.insert(cards, info.cardId)
          end
        end
        event:setCostData(self, { cards = cards, tos = { move.from }, index = i })
        return #cards > 0 --and ((#player:getCardIds("he") - #cards) > 0)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local cards = event:getCostData(self).cards
    local to = event:getCostData(self).tos[1]
    return player.room:askToSkillInvoke(player,
      { skill_name = skel.name, prompt = "#lb_zero__jingtuan_dis:" .. to.id .. "::" .. #cards })
  end,
  on_use = function(self, event, target, player, data)
    player:addSkillBranchUseHistory(skel.name, "discard")
    player.room:setPlayerMark(player, "@lb_zero__jingtuan", "discard")
    local room = player.room
    local cards = event:getCostData(self).cards
    local to = event:getCostData(self).tos[1]
    local index = event:getCostData(self).index
    local discards = table.filter(to:getCardIds("he"), function(id)
      return not (table.contains(cards, id) or to:prohibitDiscard(id))
    end)
    if #discards > #cards then
      discards = room:askToDiscard(to, {
        min_num = #cards,
        max_num = #cards,
        skill_name = skel.name,
        pattern = tostring(Exppattern { id = discards }),
        cancelable = false,
        include_equip = true,
        prompt = "#lb_zero__jingtuan_discard:::" .. #cards,
        skip = true,
      })
    end
    if #discards > 0 then
      -- 获取移动前此牌在原区域内的卡牌信息
      local markBeforeCard = function(id)
        local card = Fk:getCardById(id)
        local owner = room:getCardOwner(id)
        if owner then
          card = owner:getVirtualEquip(id) or card
        end
        local c = Fk:cloneCard(card.name, card.suit, card.number)
        local markTable = card:isVirtual() and card.mark or room.card_marks[card.id]
        if markTable then
          for k, v in pairs(markTable) do
            c.mark[k] = v
          end
        end
        return c
      end
      for i, move in ipairs(data) do
        if i == index then
          for _, id in ipairs(discards) do
            local info = { ---@type MoveInfo
              beforeCard = markBeforeCard(id),
              cardId = id,
              fromArea = player.room:getCardArea(id)
            }
            table.insert(move.moveInfo, info)
          end
        end
      end
    end
    gainAnExtraTurn_end(player, Player.Discard, skel.name)
  end,
}, { check_skill_limit = true })

skel:addAcquireEffect(function (self, player, is_start)
  player.room:setPlayerMark(player, "@lb_zero__jingtuan", player:getMark("lb_zero__jingtuan"))
end)

skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "lb_zero__jingtuan", player:getMark("@lb_zero__jingtuan"))
  player.room:setPlayerMark(player, "@lb_zero__jingtuan", 0)
end)

return skel
